package com.crawler.search.index.suffix;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.crawler.search.utils.DateParseUtil;
import com.crawler.search.utils.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * 根据类型，返回不同的索引类名
 * @author zyh
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LangSuffixer extends Suffixer {

	public String field;

	public static String lang = "_zh";

	@Override
	public String getAliasIndex(String index) {
		if(index.lastIndexOf("_")>-1){
			return index.substring(0,index.lastIndexOf("_"));
		}
		return index;
	}

	@Override
	public String createAliasName(JSONObject json) {
		return getLangSuffix(json);
	}

	/**
	* 
	* 根据日期设置索引别名后缀
	* @return {@link String}
	**/
	public  String getLangSuffix(JSONObject json){
		String suffix = lang;
		if(!StringUtils.isEmpty(field)){
			if(json.containsKey(field)){
				String suff = json.getString(field);
				if(!StringUtils.isEmpty(suff)){
					suffix = "_"+suff;
				}
			}
		}
		return suffix;
	}
}
